<template>
  <div class="cart">
    <div class="head">
      <h1 class="cart-head">
        购物车({{ shoplist.length }}) <span @click="removeItem()">删除</span>
      </h1>
      <p class="head-bar">共{{ shoplist.length }}件宝贝</p>
    </div>
    <div class="cart-item">
      <van-checkbox-group v-model="select" ref="checkboxGroup">
        <div v-for="(item, index) in shoplist" :key="item.id" class="list-item">
          <div class="cart-checkbox">
            <van-checkbox :name="item" checked-color="#FF8400"></van-checkbox>
          </div>
          <div class="cart-img">
            <img
              src="https://gw.alicdn.com/bao/uploaded/i1/2201224611422/O1CN01MKiAvE1MNJnthfQqG_!!0-item_pic.jpg_500x500q90.jpg_.webp"
              alt=""
            />
          </div>
          <div class="cart-detail">
            {{ item.tital }}
          </div>
          <div class="cart-price">￥{{ item.price * item.num }}</div>
          <div class="cart-count">
            <button @click="reduceCount(item, index)">-</button>{{ item.num
            }}<button @click="item.num++">+</button>
          </div>
        </div>
      </van-checkbox-group>
      <!-- <div class="list-item"></div>
      <div class="list-item"></div>
      <div class="list-item"></div>
      <div class="list-item"></div>
      <div class="list-item"></div>
      <div class="list-item"></div> -->
    </div>
    <van-submit-bar :price="sum * 100" button-text="结算" @submit="onSubmit">
      <van-checkbox v-model="selectAll" @change="sa()" checked-color="#FF8400"
        >全选</van-checkbox
      >
    </van-submit-bar>
  </div>
</template>

<script>
import Vue from "vue";
import { Checkbox, CheckboxGroup, SubmitBar, Dialog } from "vant";
Vue.use(Dialog);
Vue.use(Checkbox);
Vue.use(CheckboxGroup);
Vue.use(SubmitBar);
export default {
  data() {
    return {
      shoplist: [
        {
          id: 1,
          name: "商品标题",
          price: 100,
          num: 1,
          tital: "18cm16cm陶瓷奶锅不粘锅汤锅迷你小锅宝宝辅食煮牛奶小锅搪瓷20cm",
          img: "",
        },
        {
          id: 2,
          name: "商品标题",
          price: 200,
          num: 1,
          tital: "didinika宝宝辅食锅迪迪尼卡多功能婴儿不粘锅儿童煎煮一体小奶锅",
          img: "",
        },
        {
          id: 3,
          name: "商品标题",
          price: 300,
          num: 1,
          tital: "陶瓷奶锅单柄小砂锅婴儿辅食锅宝宝煮泡面热牛奶小锅家用燃气奶锅",
          img: "",
        },
      ],
      selectAll: false,
      select: [],
    };
  },
  methods: {
    onSubmit() {
      console.log(123);
      this.$router.push("/detail/" + 123);
    },
    reduceCount(item, index) {
      if (item.num == 1) {
        Dialog.confirm({
          title: "只剩最后一件商品了！",
          message: "确认要删除吗?",
        })
          .then(() => {
            this.shoplist.splice(index, 1);
            // console.log(index);
          })
          .catch(() => {
            //取消
          });
      } else {
        item.num--;
      }
    },
    sa() {
      this.selectArr = [];
      this.shoplist.forEach((item) => {
        this.selectArr.push(item);
      });
      if (this.selectAll) {
        this.select = this.selectArr;
      } else {
        this.select = [];
      }
    },
    removeItem() {
      this.select.forEach((item) => {
        this.shoplist.forEach((i) => {
          if (item === i) {
            let a = this.shoplist.indexOf(i);
            this.shoplist.splice(a, 1);
            this.select.splice(a, 1);
          }
        });
      });
    },
  },

  computed: {
    sum: function () {
      let total = 0;
      this.select.forEach((item) => {
        total += item.num * item.price;
      });
      return total;
    },
  },
  watch: {
    select: function () {
      this.selectAll = this.shoplist.length === this.select.length;
    },
  },
};
</script>

<style lang="scss" scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
body,
html {
  width: 100%;
  max-width: 768px;
  margin: 0 auto;
  font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
  top: 0;
}
.van-submit-bar {
  bottom: 50px;
  padding: 0 10px 0 10px;
  button {
    background: linear-gradient(90deg, #ff9000 0%, #ff5000 98%);
  }
}
.cart {
  height: 1460px;
  background: #f2f2f2;
  // padding: 10px;
  width: 100%;
  position: relative;
  .head {
    padding: 10px;
    height: 175px;
    color: white;
    background-image: linear-gradient(-90deg, #ff5000 0%, #ff8400 100%);
    .cart-head {
      margin: 10px 0px;
      font-size: 20px;
      span {
        font-size: 16px;
        float: right;
        font-weight: 400;
      }
    }
    .head-bar {
      font-size: 12px;
      line-height: 14px;
    }
  }
  .cart-item {
    .list-item {
      margin-top: 20px;
      background: #fff;
      border-radius: 20px;
      width: 100%;
      height: 155px;
      position: relative;
      .cart-checkbox {
        height: 100%;
        width: 40px;
        text-align: center;
        line-height: 90px;
        position: relative;
        float: left;
        .van-checkbox {
          position: absolute;
          top: 45%;
          left: 10px;
        }
      }
      .cart-img {
        width: 90px;
        height: 90px;
        float: left;
        // position: absolute;
        // top: 45%;
        margin-top: 27px;
        img {
          width: 100%;
        }
      }
      .cart-detail {
        width: 230px;
        height: 33px;
        float: left;
        margin: 20px 10px 10px 10px;
        font-size: 16px;
      }
      .cart-price {
        color: #ff5500;
        font-size: 18px;
        position: absolute;
        top: 66%;
        left: 36%;
      }
      .cart-count {
        line-height: 35px;
        width: 100px;
        height: 35px;
        border: 1px #f2f2f2 solid;
        position: absolute;
        top: 66%;
        left: 70%;
        display: flex;
        justify-content: space-between;
        button {
          width: 30px;
          height: 100%;
          color: gray;
          border: none;
        }
      }
    }
    padding: 10px;
    margin: 0 auto;
    width: 100%;
    // height: 188px;
    position: absolute;
    top: 80px;
    // z-index: 999;
  }
}
</style>